package com.xhd.serverComm;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;

import com.xhd.exception.SystemException;
import com.xhd.server.ServerConfig;
import com.xhd.util.XThread;

public class CommServerThread extends XThread
{
  private ServerSocket jm201 = null;
  private int jm202;
  private Vector jm203 = new Vector();
  private int jm178 = 0;

  public CommServerThread(int paramInt)
  {
    super(ServerConfig.SystemName + " Communication Server : " + paramInt);
    this.jm202 = paramInt;
    stopOldThread();
  }

  public void destroy()
  {
    jm200();
  }

  public void interrupt()
  {
    jm200();
    super.interrupt();
  }

  public void run()
  {
    int i = 0;
    while (i < 50)
      try
      {
        this.jm201 = new ServerSocket(this.jm202, 2);
      }
      catch (Exception localInterruptedException2)
      {
    	  localInterruptedException2.printStackTrace();
        if (hasOldThread())
          try
          {
            sleep(6000L);
          }
          catch (InterruptedException localInterruptedException3)
          {
          }
        ++i;
      }
    if (this.jm201 == null)
    {
      SystemException localSystemException = new SystemException(10040);
      localSystemException.printStackTrace();
      return;
    }
    try
    {
      sleep(10000L);
    }
    catch (InterruptedException localInterruptedException1)
    {
    }
    try
    {
     
      do
      {
        Socket localSocket = null;
        try
        {
          localSocket = this.jm201.accept();
        }
        catch (Exception localException2)
        {
          localException2.printStackTrace();
        }
        if (localSocket != null)
        {
          this.jm178 += 1;
          this.jm203.add(localSocket);
          jm199(localSocket, this.jm178);
        }
        try
        {
          sleep(6000L);
        }
        catch (InterruptedException localInterruptedException3)
        {
        }
        label151: if (this.stop);
      }
      while (this.jm201 != null);
    }
    finally
    {
      jm200();
    }
  }

  private void jm199(Socket paramSocket, int paramInt)
  {
    InputStream localInputStream = null;
    OutputStream localOutputStream = null;
    try
    {
      localInputStream = paramSocket.getInputStream();
      localOutputStream = paramSocket.getOutputStream();
    }
    catch (Exception localException1)
    {
      localException1.printStackTrace();
    }
    if ((localInputStream == null) || (localOutputStream == null))
    {
      try
      {
        paramSocket.close();
      }
      catch (Exception localException2)
      {
      }
      paramSocket = null;
    }
    if (paramSocket == null)
      return;
    String str1 = null;
    try
    {
      byte[] arrayOfByte = new byte[1024];
      int i = localInputStream.read(arrayOfByte);
      if (i > 0)
      {
        String str2 = new String(arrayOfByte, 0, i, "UTF-8");
        i = str2.indexOf("ServerId=");
        if ((i >= 0) && (i < str2.length()))
        {
          str1 = str2.substring(i + 9);
          localOutputStream.write("OK".getBytes("UTF-8"));
        }
      }
    }
    catch (Exception localException3)
    {
      localException3.printStackTrace();
    }
    if (str1 == null)
    {
      try
      {
        localInputStream.close();
        localOutputStream.close();
        paramSocket.close();
      }
      catch (Exception localException4)
      {
        localException4.printStackTrace();
      }
      return;
    }
    CommThread localCommThread = new CommThread(2, str1);
    localCommThread.SetSocket(paramSocket, localInputStream, localOutputStream);
    localCommThread.start();
  }

  private void jm200()
  {
    System.out.println("Stop " + getName());
    this.stop = true;
    if (this.jm201 == null)
      return;
    try
    {
      this.jm201.close();
    }
    catch (Exception localException1)
    {
      localException1.printStackTrace();
    }
    this.jm201 = null;
    for (int i = 0; i < this.jm203.size(); ++i)
      try
      {
        Socket localSocket = (Socket)this.jm203.elementAt(i);
        localSocket.close();
      }
      catch (Exception localException2)
      {
      }
  }
}
